package com.yh.autobio.modules.adatper;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yh.autobio.R;
import com.yh.autobio.common.StringUtils;
import com.yh.autobio.component.dialog.DialogHelp;
import com.yh.autobio.modules.bean.InvoiceBean;
import com.yh.autobio.modules.bean.ProductBean;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by john on 2018/3/6 0006. 10:58
 */

public class InvoiceDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context ctx;
    private InvoiceBean datas;
    private List<ProductBean> products;
    private static final int TYPE_ONE = 0;
    private static final int TYPE_TWO = 1;
    private static final int TYPE_THREE = 2;


    public InvoiceDetailAdapter(Context ctx) {
        this.ctx = ctx;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ONE) {
            View view = LayoutInflater.from(ctx).inflate(R.layout.fragment_invoice_head, parent,
                    false);
            return new HeadViewHolder(view);
        } else if (viewType == TYPE_TWO) {
            View view = LayoutInflater.from(ctx).inflate(R.layout.fragment_invoice_inverntory, parent,
                    false);
            return new InverntoryViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadViewHolder) {
            initHead((HeadViewHolder) holder);
        } else if (holder instanceof InverntoryViewHolder) {
            initInverntory((InverntoryViewHolder) holder);
        }
    }

    @Override
    public int getItemCount() {
        if (products != null && products.size() > 0) {
            return 1 + products.size();
        } else {
            return 1;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position == TYPE_ONE) {
            return TYPE_ONE;
        } else {
            return TYPE_TWO;
        }
    }

    public void setData(InvoiceBean data) {
        this.datas = data;
    }

    public void setProducts(List<ProductBean> datas) {
        this.products = datas;
//        notifyItemChanged(1,products.size());
        notifyDataSetChanged();
//        if(datas!=null&&datas.size()>1){
//            notifyItemRangeChanged(0, products.size()+1);
//        }else{
//            notifyItemChanged(0);
//        }

    }

    public List<ProductBean> getProducts() {
        return products;
    }

    private void initHead(HeadViewHolder holder) {
        holder.tvDjbh.setText(datas.getFbillno());
        holder.tvFhbm.setText("郑州安图生物工程股份有限公司");
        holder.tvFhrq.setText(datas.getFDate().split("T")[0]);
        holder.tvFhsd.setText(datas.getFInterval());
        holder.tvFyfs.setText(datas.getFCategory());
        holder.tvKhmc.setText(datas.getFOrganization());
        holder.tvShdh.setText(datas.getFTel());
        holder.tvShdw.setText(datas.getFReceive());
        holder.tvShlxr.setText(datas.getFContacts());
        holder.tvShdz.setText(datas.getFaddress());
        holder.tvSjh.setText(datas.getFMobile());
        if (datas.getCaseNums()!=null && datas.getCaseNums().size()>0){
            String str = "";
            for (int i = 0; i < datas.getCaseNums().size(); i++) {
                int boxNum = datas.getCaseNums().get(i);
                if (i == datas.getCaseNums().size()-1){
                    str+=boxNum;
                }else{
                    str+=boxNum+",";
                }

            }
            holder.tvXh.setText(str);
        }

    }

    private void initInverntory(final InverntoryViewHolder holder) {
        final int position = holder.getAdapterPosition();
        if (position > 0) {
            final ProductBean bean = products.get(position - 1);
            if (position == 1) {
                holder.llTitle.setVisibility(View.VISIBLE);
            } else {
                holder.llTitle.setVisibility(View.GONE);
            }
            if (bean.isChecked()) {
                holder.itemView.setBackgroundColor(Color.RED);
            } else {
                holder.itemView.setBackgroundColor(Color.WHITE);
            }
            holder.tvCpmc.setText(bean.getFEntryName());
            holder.tvGgxh.setText(bean.getFEntryModel());
            holder.tvSl.setText(bean.getFWillNum());
            holder.tvBatch.setText(bean.getFBatchNo());
            holder.etSl.setText(bean.getFDoneNumber() + "");
            holder.tvDw.setText(bean.getFUnitName());
            if (0==bean.getFBoxNum()){
                holder.tvXs.setText("");
            }else{
                holder.tvXs.setText(bean.getFBoxNum()+"");
            }

            holder.llXs.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final View view1 = View.inflate(ctx, R.layout.dialog_box_num, null);
                    final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                    final EditText content = (EditText) view1.findViewById(R.id.et_content);
                    content.setInputType(InputType.TYPE_CLASS_NUMBER);//| InputType.TYPE_NUMBER_FLAG_DECIMAL
//                    content.setText(bean.getFBoxNum()+"");
                    if (0 == bean.getFBoxNum()) {
                        content.setText("");
                        content.setHint("请输入箱数");
                    } else {
                        content.setText(bean.getFBoxNum() + "");
                        content.setSelection(content.getText().length());
                    }
//                    if (0==bean.getFDoneNumber()){
//                        content.setText("");
//                        content.setHint("请输入装箱数量");
//                    }else{
//                        content.setText(bean.getFDoneNumber()+"");
//                        content.setSelection(content.getText().length());
//                    }
//                    title.setText(bean.getFEntryName()+"["+bean.getFEntryModel()+"]");
                    DialogHelp.getConfirmDialog(ctx, view1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            String s = content.getText().toString();
                            try {
                                int v = Integer.parseInt(s);
                                products.get(position - 1).setFBoxNum(v);
                                holder.tvXs.setText(s + "");
                            } catch (Exception e) {
                                products.get(position - 1).setFBoxNum(0);
                                holder.tvXs.setText(0 + "");
                            }

                        }
                    }).show();
                }
            });

            holder.llSl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final View view1 = View.inflate(ctx, R.layout.dialog_change_mat, null);
                    final TextView title = (TextView) view1.findViewById(R.id.tv_title);
                    final TextView batch = (TextView) view1.findViewById(R.id.tv_batch);
                    final EditText content = (EditText) view1.findViewById(R.id.et_content);
                    content.setInputType(InputType.TYPE_CLASS_NUMBER);//| InputType.TYPE_NUMBER_FLAG_DECIMAL
                    if (0 == bean.getFDoneNumber()) {
                        content.setText("");
                        content.setHint("请输入装箱数量");
                    } else {
                        content.setText(bean.getFDoneNumber() + "");
                        content.setSelection(content.getText().length());
                    }
                    final TextView num = (TextView) view1.findViewById(R.id.tv_num);
                    num.setText(bean.getFWillNum() + "(待装数量)");
                    batch.setText(bean.getFBatchNo() + "  (批次)");
                    title.setText(bean.getFEntryName() + "[" + bean.getFEntryModel() + "]");
                    DialogHelp.getConfirmDialog(ctx, view1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            String s = content.getText().toString();
                            try {
                                int v = Integer.parseInt(s);
                                String fWillNum = bean.getFWillNum();
                                int i1 = (int)Double.parseDouble(fWillNum);
                                if (i1<v){
                                    Snackbar.make(((Activity)ctx).getWindow().getDecorView(),"超出待装数量！",Snackbar.LENGTH_SHORT).show();
                                    return;
                                }
                                products.get(position - 1).setFDoneNumber(v);
                                holder.etSl.setText(s);
                            } catch (Exception e) {
                                products.get(position - 1).setFDoneNumber(0);
                                holder.etSl.setText("0");
                            }

                        }
                    }).show();
                }
            });
        }
        if (listener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    listener.OnItemLong(view, holder.getAdapterPosition());
                    return false;
                }
            });
        }
    }

    private OnItemLongClickListener listener;

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemLongClickListener {
        void OnItemLong(View view, int position);
    }


    class HeadViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.tv_djbh)
        TextView tvDjbh;
        @InjectView(R.id.tv_shdw)
        TextView tvShdw;
        @InjectView(R.id.ll_gh_unit)
        LinearLayout llGhUnit;
        @InjectView(R.id.tv_fhrq)
        TextView tvFhrq;
        @InjectView(R.id.tv_fhsd)
        TextView tvFhsd;
        @InjectView(R.id.tv_fyfs)
        TextView tvFyfs;
        @InjectView(R.id.tv_khmc)
        TextView tvKhmc;
        @InjectView(R.id.ll_pgno)
        LinearLayout llPgno;
        @InjectView(R.id.tv_fhbm)
        TextView tvFhbm;
        @InjectView(R.id.tv_shlxr)
        TextView tvShlxr;
        @InjectView(R.id.ll_sale)
        LinearLayout llSale;
        @InjectView(R.id.ll_caseNum)
        LinearLayout llCaseNum;
        @InjectView(R.id.tv_shdh)
        TextView tvShdh;
        @InjectView(R.id.tv_xh)
        TextView tvXh;
        @InjectView(R.id.tv_shdz)
        TextView tvShdz;
        @InjectView(R.id.tv_sjh)
        TextView tvSjh;


        public HeadViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    class InverntoryViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.ll_title)
        LinearLayout llTitle;
        @InjectView(R.id.tv_cpmc)
        TextView tvCpmc;
        @InjectView(R.id.tv_ggxh)
        TextView tvGgxh;

        @InjectView(R.id.tv_sl)
        TextView tvSl;
        @InjectView(R.id.et_sl)
        TextView etSl;
        @InjectView(R.id.tv_batch)
        TextView tvBatch;
        @InjectView(R.id.tv_dw)
        TextView tvDw;
        @InjectView(R.id.tv_xs)
        TextView tvXs;
        @InjectView(R.id.ll_sl)
        LinearLayout llSl;
        @InjectView(R.id.ll_xs)
        LinearLayout llXs;
        @InjectView(R.id.ll_content)
        LinearLayout llContent;

        public InverntoryViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }
}
